#ifndef __APP_KEY_H__
#define __APP_KEY_H__

#include "bsp_adc.h"
#include "bsp_can.h"
#include "bsp_pwm.h"
#include "bsp_flash.h"
#include "bsp_timer.h"
#include "bsp_led.h"
#include "reset.h"
#include "bsp_key.h"
#include <stdbool.h>

#define SLIDE_RES_AD_MAX (4085u)

#define SLIDE_RES_Rvalue_B (3340u)
#define SLIDE_RES_Rvalue_C  (385u)
#define SLIDE_RES_Rvalue_D (2u)
    
#define  VERSION_BRAKE_DEEP 

#define Set_IO_LoadDischarge  	Gpio_SetIO(GpioPortB, GpioPin14)
#define Clr_IO_LoadDischarge    Gpio_ClrIO(GpioPortB, GpioPin14)

#define Set_IO_5VPulse  	Gpio_SetIO(GpioPortA, GpioPin8)
#define Clr_IO_5VPulse   	Gpio_ClrIO(GpioPortA, GpioPin8)

#define Set_IO_PWM  	Gpio_SetIO(GpioPortA, GpioPin0)
#define Clr_IO_PWM   	Gpio_ClrIO(GpioPortA, GpioPin0)

typedef enum
{
	PULSE,
	PWM,
}e_pwm_mod;

extern e_pwm_mod PWM_Mode;

typedef enum
{
	AB, /* Max*/
	BC, /* midia*/
    CD, /* pulse interval*/
}e_res_pos;

typedef struct stc_slide_res
{
	uint16_t Rvalue ;
    e_res_pos  Pos ; 
    uint8_t Pos_BC_index; /*gain in bc : [0~A]*/
    boolean_t pos_changed;
}stc_slide_res_t;

extern stc_slide_res_t slide_res;

typedef struct Power
{
	uint16_t voltage; /*mV*/
	uint16_t current; /* mA */
}Power_t;

extern Power_t Pwr;

void PWMPin_reinit_NormalPin(void);

void Pulse_5V_12V_Output(void);

void Pulse_5V_Closed(void);

void App_key_process(void);

void Slide_resistor_detect(void);	

void Update_BrakeGain_buffer(void);

void PWM_duty_adjust(uint16_t adc_value, uint16_t MAX_Value); // 输入参数: 滑动变阻器ad值或can接收刹车值

void Load_Current_detect(void);

void Over_Voltage_detect(void);

void App_Struct_data_init(void);

void PWM_mod_change(e_pwm_mod mdoe);

void CAN_BUS_Process(void);

void PWM_Output1(void);

bool  SlideRes_in_BC_Period(void);

#endif
